<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="家乡ID" prop="hometownId">
        <el-input
          v-model="queryParams.hometownId"
          placeholder="请输入家乡ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="家乡名称" prop="hometownName">
        <el-input
          v-model="queryParams.hometownName"
          placeholder="请输入家乡名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="家乡创建人昵称" prop="createUserName" label-width="100">
        <el-input
          v-model="queryParams.createUserName"
          placeholder="请输入家乡创建人昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="家乡创建人ID" prop="createUserId" label-width="100">
        <el-input
          v-model="queryParams.createUserId"
          placeholder="请输入家乡创建人ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="hometowList" @selection-change="handleSelectionChange">
      <el-table-column label="家乡ID" align="center" prop="hometownId" width="180"/>
      <el-table-column label="家乡名称" align="center" prop="hometownName" />
      <el-table-column label="家乡状态" align="center" prop="hometownStatus" width="80">
        <template v-slot="scope">
          <dic-staus-tag :options="hometownStatusTag" :staus="scope.row.hometownStatus"></dic-staus-tag>
        </template>
      </el-table-column>
      <el-table-column label="家乡创建人昵称" align="center" prop="createUserName" />
      <el-table-column label="家乡创建人ID" align="center" prop="createUserId" />
      <el-table-column label="家乡推荐官" align="center" prop="recommendUserName" />
      <el-table-column label="家乡创建时间" align="center" prop="createTime" />
      <el-table-column label="家乡绑定人数" align="left" prop="bindNum" width="150">
        <template slot-scope="scope"><span class="loss-num">{{ scope.row.bindNum }}</span></template>
      </el-table-column>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template v-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['system:hometow:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['system:hometow:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改家乡管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="家乡介绍" prop="hometownIntroduce">
          <el-input v-model="form.hometownIntroduce" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="家乡封面" prop="hometownCover">
          <el-input v-model="form.hometownCover" placeholder="请输入家乡封面" />
        </el-form-item>
        <el-form-item label="家乡地址" prop="hometownAddress">
          <el-input v-model="form.hometownAddress" placeholder="请输入家乡地址" />
        </el-form-item>
        <el-form-item label="家乡-省" prop="hometownProvinceId">
          <el-input v-model="form.hometownProvinceId" placeholder="请输入家乡-省" />
        </el-form-item>
        <el-form-item label="家乡-市" prop="hometownCityId">
          <el-input v-model="form.hometownCityId" placeholder="请输入家乡-市" />
        </el-form-item>
        <el-form-item label="家乡-县" prop="hometownCountyId">
          <el-input v-model="form.hometownCountyId" placeholder="请输入家乡-县" />
        </el-form-item>
        <el-form-item label="家乡-镇" prop="hometownTownId">
          <el-input v-model="form.hometownTownId" placeholder="请输入家乡-镇" />
        </el-form-item>
        <el-form-item label="家乡-乡" prop="hometownVillageId">
          <el-input v-model="form.hometownVillageId" placeholder="请输入家乡-乡" />
        </el-form-item>
        <el-form-item label="家乡创建人" prop="createUserId">
          <el-input v-model="form.createUserId" placeholder="请输入家乡创建人" />
        </el-form-item>
        <el-form-item label="家乡推荐官" prop="recommendUserId">
          <el-input v-model="form.recommendUserId" placeholder="请输入家乡推荐官" />
        </el-form-item>
        <el-form-item label="创建者" prop="createBy">
          <el-input v-model="form.createBy" placeholder="请输入创建者" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker clearable size="small"
            v-model="form.createTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新者" prop="updateBy">
          <el-input v-model="form.updateBy" placeholder="请输入更新者" />
        </el-form-item>
        <el-form-item label="更新时间" prop="updateTime">
          <el-date-picker clearable size="small"
            v-model="form.updateTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    addHometow,
    delHometow,
    getHometow,
    listHometow,
    updateHometow
  } from "@/api/store/hometown";

  export default {
  name: "Hometown",
  data() {
    return {
      hometownStatusTag: [
        { value: 0, label: "已开通", cssClass: "success" },
        { value: 1, label: "未开通", cssClass: "warning" }
      ],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 家乡管理表格数据
      hometowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hometownName: undefined,
        hometownIntroduce: undefined,
        hometownCover: undefined,
        hometownAddress: undefined,
        hometownStatus: undefined,
        hometownProvinceId: undefined,
        hometownCityId: undefined,
        hometownCountyId: undefined,
        hometownTownId: undefined,
        hometownVillageId: undefined,
        createUserId: undefined,
        recommendUserId: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询家乡管理列表 */
    getList() {
      this.loading = true;
      listHometow(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.hometowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    addDateRange(params, dateRange) {
      let search = params;
      dateRange = Array.isArray(dateRange) ? dateRange : [];
      search['startDate'] = dateRange[0];
      search['endDate'] = dateRange[1];
      return search;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        hometownId: undefined,
        hometownIntroduce: undefined,
        hometownCover: undefined,
        hometownAddress: undefined,
        hometownStatus: 0,
        hometownProvinceId: undefined,
        hometownCityId: undefined,
        hometownCountyId: undefined,
        hometownTownId: undefined,
        hometownVillageId: undefined,
        createUserId: undefined,
        recommendUserId: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.hometownId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加家乡管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const hometownId = row.hometownId || this.ids
      getHometow(hometownId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改家乡管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.hometownId != null) {
            updateHometow(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addHometow(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const hometownIds = row.hometownId || this.ids;
      this.$modal.confirm('是否确认删除家乡管理编号为"' + hometownIds + '"的数据项？').then(() => {
        this.loading = true;
        return delHometow(hometownIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/hometow/export', {
        ...this.queryParams
      }, `hometow_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>

.loss-num {
  color: #67C23A;
  font-weight: bold;
}

</style>
